var tempdata = require('../../utils/tempdata.js');
var utils = require('../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopinfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          scrollhgt:res.windowHeight
        });
      },
    });
    this.loadToServer();
  },
  loadToServer: function () {
    var url = app.globalData.webUrl + 'Store/storeindex';
    app.loadDataFromServer(url, this.loadCallBack, {});
  },
  loadCallBack: function (res) {

    wx.hideToast();
    if (res.status != 'ok') {
      wx.showToast({
        title: '网络错误~',
        image: '../imgs/tips.png'
      });
      return;
    }

    var shopinfo = res.result;
    var adlist = res.result.adinfo;
    var toplist = [];
    var yhlist = [];
    for (var i = 0; i < adlist.length; i++) {
      adlist[i].icon = app.globalData.ImgwebUrl + adlist[i].icon;
      if (adlist[i].atype == 8){
        adlist.push(adlist[i]);
      }
      else{
        yhlist.push(adlist[i]);
      }      
    }
    
    var imgs = app.repicSrc(shopinfo.sinfo.htmlmsg);
    shopinfo.sinfo.details = imgs;

    shopinfo.sinfo.icon = app.globalData.ImgwebUrl + shopinfo.sinfo.icon;
    shopinfo.sinfo.qrcode = app.globalData.ImgwebUrl + shopinfo.sinfo.qrcode;
    shopinfo.sinfo.addressicon = app.globalData.ImgwebUrl + shopinfo.sinfo.addressicon;
    
    shopinfo.toplist = toplist;
    shopinfo.yhlist = yhlist;

    console.log('sinfo=' + shopinfo.sinfo.phone);
    this.setData({
      shopinfo: shopinfo
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      shoppics: tempdata.shoppics,
      showModel: false
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  openMap: function () {
    var that = this;
    var pos = utils.transferMarchPos(121.474891, 31.250006);
    console.log(pos);
    wx.openLocation({
      latitude: Number(pos[0]),
      longitude: Number(pos[1]),
      success: function (res) {

      },
      fail: function (res) {
        that.checkLocationAuthor();
      }
    });
  },
  checkLocationAuthor: function () {
    var that = this;
    wx.getSetting({
      success: (res) => {
        console.log(res);
        console.log(res.authSetting['scope.userLocation']);
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {//非初始化进入该页面,且未授权
          that.setData({
            showModel: true
          });
        } else if (res.authSetting['scope.userLocation'] == undefined) {//初始化进入
          console.log('初始化进入');
        }
      }
    });
  },
  agreeGetLocation: function (e) {
    console.log(e);
  },
  showDetailPic:function(e){
    var picurl = e.currentTarget.dataset.picurl;
    wx.previewImage({
      current:picurl,
      urls: this.data.shopinfo.sinfo.details,
    });
  },
  scrollCnt:function(e){
    var cntid = e.currentTarget.dataset.cntid;
    this.setData({
      viewflag:cntid
    });
  }
})